<?php get_instance()->view('common/header');?>
<div class="page-container home">
    <?php get_instance()->view('common/sidebar');?>
    <div class="page-content-wrapper">
        <div class="page-content">
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <h3 class="page-title"> <?php if($is_add):?>新增管理员 <?php else:?>编辑管理员<?php endif;?> </h3>
                    <ul class="page-breadcrumb breadcrumb">
                        <li class="btn-group"> <a class="btn default ml_5 return" href="<?php echo getUrl('config/county');?>"><i class="fa fa-chevron-left"></i> 返回</a> </li>
                        <li><i class="fa fa-home"></i><a href="<?php echo getUrl('setting/user');?>">管理员 <i class="fa fa-angle-right"></i></a></li>
                        <li><a href="javascript:void(0);"><?php if($is_add):?>新增 <?php else:?>编辑<?php endif;?></a></li>
                    </ul>
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <!--开始-->

            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN VALIDATION STATES-->
                    <div class="portlet box">
                        <div class="portlet-body form">
                            <!-- BEGIN FORM-->
                            <form action="" class="form-horizontal" method="post" novalidate="novalidate">
                                <div class="form-body">
                                    <div class="form-group">
                                        <label class="control-label col-md-3">管理员名称 <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <input maxlength="12" class="form-control required" name="manager" value="<?php echo  $is_add ? '' : $admin['manager'];?>" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3">区域名称 <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <input maxlength="20" class="form-control required" name="section_name" value="<?php echo  $is_add ? '' : $admin['section_name'];?>" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group">
		                                <label class="control-label col-md-3">区域： </label>
		                                <div class="col-md-8">
			                                <a class="add-btn" href="javascript:void(0);"><i class="fa fa-plus"></i> 添加</a>
			                                <script type="text/javascript">
												$(function(){
													$('.add-btn').click(function(){

														var index = 0;
														$('.area-row').each(function(){
															if($(this).attr('index') > index){
																index = parseInt($(this).attr('index'));
															}
														});
														addRow((index+1));
													});

													<?php if( ! $is_add):?>
														var data = eval('(<?php echo json_encode($admin['area'])?>)');
														if(data.length){
															$.each(data, function(k,v){
																addRow(k, v.province_id, v.city_id, v.county);
															});
														}else{
															addRow(0, 0, 0, 0, null);
														}
													<?php else:?>
													addRow(0, 0, 0, 0, null);
													<?php endif;?>
												});

												function addRow(index, province, city, county){
													var html = '<div class="row area-row" style="margin-bottom:10px;" index="'+index+'">';
					                                html += '<div class="col-md-3">';
					                                html += '<select class="form-control province required" placeholder="省份" name="province['+index+']">';
					                                html += '<option value=""></option>';
					                                   	   <?php foreach ($province as $v):?>
					                                   	   
					                                html += '<option value="<?php echo $v['id'];?>"><?php echo $v['name'];?></option>';
					                                   	   <?php endforeach;?>
					                                html += '</select>';
					                                html += '</div>';
					                                html += '<div class="col-md-3">';
					                                html += '<select class="form-control city required" placeholder="城市" name="city['+index+']">';
					                                html += '<option value=""></option>';
					                                html += '</select>';
					                                html += '</div>';
					                                html += '<div class="col-md-3">';
					                                html += '<select class="form-control county required" placeholder="区县" multiple name="county['+index+'][]"></select>';
					                                html += '<a href="javascript:void(0);" class="delete-row" style="position:absolute;right:-10px;top:7px;"><i class="fa fa-times"></i></a></div>';
					                                html += '</div>';	
					                                var obj = $(html);	
					                                obj.find('select').select2();
					                                obj.find('.delete-row').click(function(){
														$(this).parents('.area-row').remove();
						                            });

					                                $('.add-btn').before(obj);
					                                localtion.init({city:city, county:county, boxId:'.area-row[index="'+index+'"]',url:"<?php echo getUrl('config/county/getAreaByPid');?>"});
					                                if(province > 0){
						                            	obj.find('select.province')	.val(province).change();
							                        }
												}
			                                </script>
		                                </div>
		                           </div> 
                                    <div class="form-group">
                                        <label class="control-label col-md-3">电话号码  <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <input maxlength="20" class="form-control required number isMobile" mobile="true" name="mobile" value="<?php echo ! $is_add ? $admin['mobile'] : '';?>" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3">登录帐号  <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <input maxlength="15" class="form-control required" name="account" value="<?php echo ! $is_add ? $admin['account'] : '';?>" type="text">
                                        </div>
                                    </div>
                             		<div class="form-group">
                                        <label class="control-label col-md-3">状态 <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <div class="radio-list">
                                                <label class="radio-inline"><input name="status" <?php if(( ! $is_add && $admin['status'] == 2) || $is_add):?> checked<?php endif;?> value="2" type="radio"> 启用 </label>
                                                <label class="radio-inline"><input name="status" <?php if( ! $is_add && $admin['status'] == 1):?> checked<?php endif;?> value="1" type="radio"> 禁用 </label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3">密码 <?php echo ! $is_add ? '':'<span class="required">*</span>';?></label>
                                        <div class="col-md-6">
                                            <input class="form-control <?php echo ! $is_add ? '':'required';?>" id="password" minlength="6" maxlength="15" name="password" value="" type="password">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3">确认密码  <?php echo ! $is_add ? '':'<span class="required">*</span>';?></label>
                                        <div class="col-md-6">
                                            <input class="form-control <?php echo ! $is_add ? '':'required';?>" equalto="#password" name="repassword" value="" type="password">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-actionsc fluid">
                                    <div class="col-md-offset-3 col-md-9">
                                    	<input type="hidden" name="id" value="<?php echo  $is_add ? '' : $admin['id'];?>">
                                        <button type="submit" class="btn green"><i class="fa fa-save"></i> 保存</button>
                                        <a href="<?php echo getUrl('config/county');?>" class="btn default"><i class="fa fa-chevron-left"></i> 返回</a>
                                    </div>
                                </div>
                            </form>
                            <!-- END FORM-->
                        </div>
                    </div>
                    <!-- END VALIDATION STATES-->
                </div>
            </div>

            <!--结束-->
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function(){
    	$('select').select2();
        $.e_validate.init('.form-horizontal',{});
        // 手机号码验证
        jQuery.validator.addMethod("isMobile", function(value, element) {
            var length = value.length;
            var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "请正确填写您的手机号码");

        $(".form-horizontal").validate({
            rules : {
                account : {
                    required : true,
                    minlength : 11,
                    isMobile : true
                }
            },
            messages : {
                account : {
                    required : "请输入手机号",
                    minlength : "确认手机不能小于11个字符",
                    isMobile : "请正确填写您的手机号码"
                }
            },
            errorPlacement : function(error, element) {
                error.appendTo(element.next().next());
            },
            ignore : ".codeCls"
        });
    });
</script>
</body>
</html>